package com.dp.web.controller;

import com.alibaba.excel.EasyExcel;
import com.dp.entity.OrderDetails;
import com.dp.entity.OrderTable;
import com.dp.entity.ShoppingCart;
import com.dp.service.OrderDetailsService;
import com.dp.service.OrderTableService;
import com.dp.service.impl.OrderDetailsServiceImpl;
import com.dp.service.impl.OrderTableServiceImpl;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/order")
public class OrderTableController extends BaseController{

    @Autowired
    private OrderTableService service = new OrderTableServiceImpl();

    @Autowired
    private OrderDetailsService serviceDetails = new OrderDetailsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");

        String op = req.getParameter("op");
        if("finds".equals(op)){
            doFinds(req,resp);
        } else if("addOrder".equals(op)){
            doAdd(req,resp);
        }else if("findprice".equals(op)){
            doFindprice(req,resp);
        }
        else{
            printToJson(resp,"NO method ......");
        }
    }

    private void doFindprice(HttpServletRequest req, HttpServletResponse resp) throws IOException {



        Map<String,Object> map =parseRequest(req);

        Map<String,Object> m =service.findprice(map);
        printToJson(resp,m);

    }

    private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        OrderTable orderTable = parseRequest(req,OrderTable.class);
        OrderDetails orderDetails = parseRequest(req,OrderDetails.class);

        //发货状态
        orderTable.setOState(0);

       // 先插入OrderTable，获取自增的oId赋给
        printToJson(resp,service.add(orderTable));
        orderDetails.setOId(orderTable.getOId());
        //再插入OrderDetails
        printToJson(resp,serviceDetails.add(orderDetails));


        //        OrderTable orderTable = new OrderTable();
//        orderTable.setUId(Integer.valueOf(req.getParameter("uId")));
//        orderTable.setAId(Integer.valueOf(req.getParameter("aId")));
//        orderTable.setTotalPrice(Integer.valueOf(req.getParameter("totalPrice")));
        //发货状态
//        orderTable.setOState(0);

//        printToJson(resp,service.add(orderTable));
//
//        OrderDetails orderDetails = new OrderDetails();
//        orderDetails.setOId(orderTable.getOId());
//        orderDetails.setParamId(Integer.valueOf(req.getParameter("paramId")));
//        orderDetails.setPNumber(Integer.valueOf(req.getParameter("pNumber")));
//        orderDetails.setOPrice(Integer.valueOf(req.getParameter("oPrice")));
//        printToJson(resp,serviceDetails.add(orderDetails));


    }

    private void doFinds(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        OrderTable order = parseRequest(req,OrderTable.class);

        int uId = Integer.parseInt(req.getParameter("uId"));
        order.setUId(uId);

        printToJson(resp,service.finds(order));
    }

    /**
     excel文件的下载
     */
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
        //EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
    }

}
